<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\ContactStage\Action\Dashboard;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\Utils\Codec\Json;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Rbac\Middleware\PermissionMiddleware;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Plugin\ContactStage\Contract\ContactStageContract;

/**
 * 客户阶段编辑.
 *
 * Class Index.
 * @Controller
 */
class Update extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var ContactStageContract
     */
    protected $contactStageService;

    /**
     * @RequestMapping(path="/dashboard/contactStage/update", methods="PUT")
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     *     @Middleware(PermissionMiddleware::class)
     * })
     * @return array 返回数组
     */
    public function handle(): array
    {
        $this->validated($this->request->all());

        $user = user();

        $stageId = (int) $this->request->input('stageId');
        // 接收参数
        if (! empty($this->request->input('name'))) {
            $params['name'] = $this->request->input('name');

            $data = $this->contactStageService->getContactStageByCorpIdNameId($user['corpIds'][0], $params['name'], $stageId);
            if ($data) {
                throw new CommonException(ErrorCode::INVALID_PARAMS, '已有该阶段名称，请勿重复添加');
            }
        }
        $description = $this->request->input('description');
        if (isset($description)) {
            $params['description'] = $description;
        }
        if (! empty($this->request->input('order'))) {
            $params['order'] = $this->request->input('order');
        }
        $autoAttribute = $this->request->input('autoAttribute');
        if (isset($autoAttribute)) {
            $params['auto_attribute'] = Json::encode($this->request->input('autoAttribute'));
        }

        $remindAttribute = $this->request->input('remindAttribute');
        if (isset($remindAttribute)) {
            $params['remind_attribute'] = Json::encode($this->request->input('remindAttribute'));
        }

        try {
            if (! empty($params)) {
                $this->contactStageService->updateContactStageById($stageId, $params);
            }
        } catch (\Exception $e) {
            throw new CommonException(ErrorCode::SERVER_ERROR, '修改失败' . $e);
        }

        return [];
    }

    /**
     * 验证规则.
     *
     * @return array 响应数据
     */
    protected function rules(): array
    {
        return [
            'stageId' => 'required | integer | bail',
        ];
    }

    /**
     * 验证错误提示.
     * @return array 响应数据
     */
    protected function messages(): array
    {
        return [
            'stageId.required' => '阶段id 必填',
            'stageId.integer' => '阶段id 必须为整型',
        ];
    }
}
